<template>
	<view class="wrap">
		<view class="top">
			<!-- <image class="top-img" src="@/static/ucenter/jb.png"></image> -->
		</view>
		<view class="tel">
			<u-form :model="form">
				<u-form-item label="手机号码" label-width='140rpx'>
					<u-input v-model="form.phone" />
				</u-form-item>
			</u-form>
		</view>
		<view class="btn">
			<button type="primary" @click="submit">确认绑定</button>
		</view>
	</view>
</template>

<script>
	import {
		setPhone
	} from '@/common/api/school.js';
	import baseUrl from '@/common/config.js'

	export default {
		data() {
			return {
				isEdit: false,
				form: {},
				userInfo: {}
			}
		},
		computed: {},
		onLoad(e) {
			if (e.isEdit) {
				this.isEdit = e.isEdit
			}
		},
		mounted() {
			this.userInfo = uni.getStorageSync('wxInfo')
		},
		created() {},
		methods: {
			submit() {
				let _this = this
				let phone = this.form.phone
				if (this.$s.isBlank(phone)) {
					this.$s.nShowToast('手机号不能为空')
					return
				}
				if (!this.$u.test.mobile(phone)) {
					this.$s.nShowToast('手机号格式不正确')
					return
				}
				let token = uni.getStorageSync('Access-Token')
				if (this.isEdit) {
					this.userInfo.phone = this.form.phone
					uni.request({
						url: baseUrl + '/api/WxUser/edit',
						header: {
							'Authorization': 'Bearer ' + token
						},
						data: this.userInfo,
						method: 'POST',
						success(res) {
							let data = res.data
							if (data.code == 200) {
								_this.$s.sShowToast('绑定成功')
							}
						}
					})
				} else {
					this.form.token = token
					uni.request({
						url: baseUrl + '/api/Account/setPhone',
						data: this.form,
						success(res) {
							let data = res.data
							if (data.code == 200) {
								_this.$s.sShowToast('绑定成功')
							}
							uni.setStorage({
								key: 'Access-Token',
								data: data.data
							});
							uni.setStorage({
								key: 'wxInfo',
								data: data.remark
							});
						}
					})
				}
				uni.redirectTo({
					url: '/pages/home/index'
				})
			}
		}
	}
</script>

<style>

</style>

<style lang="scss" scoped>
	.top {
		line-height: 260rpx;
		height: 260rpx;
		text-align: center;
		// vertical-align: middle;

		.top-img {
			width: 130upx;
			height: 130upx;
		}
	}

	.tel {
		height: 100rpx;
		text-align: center;
		font-size: 35rpx;
		padding-left: 80rpx;
		padding-right: 80rpx;
	}

	.btn {
		margin: 60rpx auto;
		width: 80%;
	}
</style>
